package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.hjq.fcloud.R
import com.hjq.fcloud.bean.RegulatoryMaterialConfirmationDetailBean
import com.hjq.fcloud.bean.FlowDataBean
import com.hjq.fcloud.config.FlowStatusConfig
import com.hjq.fcloud.event.EventBean
import com.hjq.fcloud.event.EventBusUtils
import com.hjq.fcloud.http.api.RegulatoryMaterialConfirmationApi
import com.hjq.fcloud.http.api.RegulatoryMaterialConfirmationSignApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.other.AppFilesHelper
import com.hjq.fcloud.widget.BottomSignViewClickListener
import com.hjq.http.EasyHttp
import com.hjq.http.body.JsonBody
import com.hjq.http.listener.HttpCallback
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.bottomView
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.buildCorpNameSb
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.constructCorpNameSb
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.signModeSb
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.feedbackRootView
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.feedbackView
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.fileView
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.flowView
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.loanSb
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.moneySb
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.noSb
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.projectNameSb
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.sectNameSb
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.statusLayout
import kotlinx.android.synthetic.main.activity_regulatory_material_confirmation.supplyCorpNameSb
import kotlinx.android.synthetic.main.layout_sign_result.signResultIv
import okhttp3.Call
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 监管材料确权签约
 */
class RegulatoryMaterialConfirmationActivity : BaseDetailActivity() {


    companion object {
        //1通过0不通过
        const val STATUS_PASS = "1"
        const val STATUS_NOT_PASS = "0"

        fun start(ac: Context, jumpBean: CommonJumpBean,isDisableAgainSign:Boolean = false) {
            var intent = Intent(ac, RegulatoryMaterialConfirmationActivity::class.java)
            intent.putExtra(INTENT_IN_ITEM, jumpBean)
            intent.putExtra(INTENT_IN_DISABLE_AGAIN_SIGN, isDisableAgainSign)
            ac.startActivity(intent)
        }
    }

    private var mDetail: RegulatoryMaterialConfirmationDetailBean? = null
    private var status: String? = STATUS_PASS

    override fun userEventBus(): Boolean {
        return true
    }

    override fun getStateLayout(): View? {
        return statusLayout
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_regulatory_material_confirmation
    }

    override fun initView() {

    }

    override fun initData() {
        getServerFlow()

        getServerFlowStatus({ flowStatusBean ->
            if(flowStatusBean.isFlowUnfinished){
                showFlowUnfinishedView(flowStatusBean.flowUnfinishedTipsArray)
                return@getServerFlowStatus
            }

            getServerDetail()

            if (flowStatusBean.canSign || flowStatusBean.canAgainSign) {
                var isAgainSign = flowStatusBean.canAgainSign && !isDisableAgainSign//重新签约
                if (isAgainSign) {//需要展示流程和签约方式
                    flowView.setVisibility(true)
                    //签约方式
                    signModeSb.setVisibility(true)
                } else {
                    //审核控件设置
                    feedbackRootView.setVisibility(true)
                }
                //底部签约控件设置
                bottomView.setViewData(isAgainSign, object :
                    BottomSignViewClickListener {
                    override fun onAgainPassSign() {
                        requestServerResetFlow() {
                            start(context, commonJumpBean!!, true)
                            finish()
                        }
                    }

                    override fun onPassSign() {
                        status = STATUS_PASS
                        if (checkInput()) {
                            FaceDetectRGBActivity.launcher(context, intent.extras)
                        }
                    }

                    override fun onNoPassSign() {
                        status = STATUS_NOT_PASS
                        if (checkInput()) {
                            FaceDetectRGBActivity.launcher(context, intent.extras)
                        }
                    }

                })
            } else {
                flowView.setVisibility(true)
                //签约方式
                signModeSb.setVisibility(true)
            }
        })
    }

    private fun getServerDetail() {
        var api = RegulatoryMaterialConfirmationApi()
            .setId(commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(api)
            .request(object :
                HttpCallback<HttpData<RegulatoryMaterialConfirmationDetailBean?>?>(this) {
                override fun onStart(call: Call?) {}
                override fun onSucceed(data: HttpData<RegulatoryMaterialConfirmationDetailBean?>?) {
                    mDetail = data?.getData()
                    setViewData(mDetail)
                }
            })
    }

    override fun setFlowViewData(data: FlowDataBean?) {
        flowView.setViewData(data?.setup_list)
    }

    private fun setViewData(item: RegulatoryMaterialConfirmationDetailBean?) {
        showAppContent()

        if(!isDisableAgainSign){
            signResultIv.setViewData(flowStatusData)
        }
        noSb.setRightText(item?.code)
        projectNameSb.setRightText(item?.project?.project_name)
        sectNameSb.setRightText(item?.sect?.sect_name)
        loanSb.setRightText(item?.loanName)

        constructCorpNameSb.setRightText(item?.construct_corp?.corp_name)
        buildCorpNameSb.setRightText(item?.build_corp?.corp_name)
        supplyCorpNameSb.setRightText(item?.supply_corp?.corp_name)
        moneySb.setRightText(item?.money)
        signModeSb.setRightText(item?.debtConfirmModeName)

        fileView.setViewData(this, FlowStatusConfig.getFileTitleName(flowStatusData), AppFilesHelper.getAllFiles(item?.files))
    }

    private fun checkInput(): Boolean {
        if (TextUtils.isEmpty(feedbackView.getInPutText())) {
            showToast("请输入意见反馈")
            return false
        }
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMainEventBus(bean: EventBean) {
        if (bean != null && bean.type == EventBusUtils.TYPE_FACE_AND_SIGN_FINISH) {
            var parms = bean.parms as HashMap<String, String?>
            requestServerConfirm(parms)
        }
    }

    private fun requestServerConfirm(signParms: HashMap<String, String?>) {
        var parms = HashMap<String, String?>()
        if (signParms != null) {
            parms.putAll(signParms)
        }
        parms.put("id", commonJumpBean?.bizId)
        parms.put("reason", feedbackView.getInPutText())
        parms.put("result", status)
        parms.put("biz_code", commonJumpBean?.bizCode)
        parms.put("biz_id", commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(RegulatoryMaterialConfirmationSignApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<Any?>?>(this) {
                override fun onSucceed(data: HttpData<Any?>?) {
                    showToast(getString(R.string.string_sign_success_tips))
                    finish()
                }
            })
    }
}